<template>
  <div class="container">
    <div class="top">
      <span>金额：</span>
      <el-input
        style="width: 200px"
        v-model="price"
        :disabled="true"
      ></el-input>
      <el-input
        style="width: 200px"
        v-model="search.searchCode"
        placeholder="请输入结算单号"
      ></el-input>
      <el-input
        style="width: 200px"
        v-model="search.pay_serial_code"
        placeholder="请输入付款单号"
      ></el-input>
      <el-select
        v-model="search.area"
        filterable
        remote
        center
        reserve-keyword
        placeholder="请输入区域"
        :remote-method="insideRemoteMethods"
        :loading="insideArea.loading"
      >
        <el-option
          v-for="item in insideArea.options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-input
        style="width: 300px"
        :disabled="true"
        placeholder="请搜索供应商信息"
        v-model="supplierNameList.name"
        v-if="!showSuppler"
      >
        <template slot="prepend"
          >供应商</template
        >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="supplierBtn"
        ></el-button>
      </el-input>
      <el-input
        style="width: 300px"
        :disabled="true"
        placeholder="请搜索商品信息"
        v-model="commodityNameList.name"
      >
        <template slot="prepend"
          >商品</template
        >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="commodityBtn"
        ></el-button>
      </el-input>
      <el-input
        style="width: 300px"
        class="inp"
        :disabled="true"
        placeholder="请搜索类别信息"
        v-model="classNameList.name"
      >
        <template slot="prepend"
          >类别</template
        >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="classBtn"
        ></el-button>
      </el-input>
      <el-select
        multiple
        filterable
        allow-create
        default-first-option
        v-model="search.examine_status"
        placeholder="请选择审核状态"
      >
        <el-option
          v-for="item in option"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <div style="display: inline-block">
        创建时间：
        <el-date-picker
          value-format="yyyy-MM-dd"
          v-model="date"
          class="hidden-xs-only"
          type="daterange"
          :clearable="false"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </div>
      <div class="datePicker hidden-sm-and-up">
        <el-date-picker
          value-format="yyyy-MM-dd"
          style="width: 150px"
          v-model="date[0]"
          type="date"
          placeholder="选择开始日期"
        ></el-date-picker
        >&nbsp;-
        <el-date-picker
          value-format="yyyy-MM-dd"
          style="width: 150px"
          v-model="date[1]"
          type="date"
          placeholder="选择结束日期"
        ></el-date-picker>
      </div>
      <div style="display: inline-block">
        供应商审核时间：
        <el-date-picker
          value-format="yyyy-MM-dd"
          v-model="dates"
          class="hidden-xs-only"
          type="daterange"
          :clearable="false"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </div>
      <div class="datePicker hidden-sm-and-up">
        <el-date-picker
          value-format="yyyy-MM-dd"
          style="width: 150px"
          v-model="dates[0]"
          type="date"
          placeholder="选择开始日期"
        ></el-date-picker
        >&nbsp;-
        <el-date-picker
          value-format="yyyy-MM-dd"
          style="width: 150px"
          v-model="dates[1]"
          type="date"
          placeholder="选择结束日期"
        ></el-date-picker>
      </div>
      <div style="display: inline-block">
        付款时间：
        <el-date-picker
          value-format="yyyy-MM-dd"
          v-model="payDate"
          class="hidden-xs-only"
          type="daterange"
          :clearable="false"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </div>
      <div class="datePicker hidden-sm-and-up">
        <el-date-picker
          value-format="yyyy-MM-dd"
          style="width: 150px"
          v-model="payDate[0]"
          type="date"
          placeholder="选择开始日期"
        ></el-date-picker
        >&nbsp;-
        <el-date-picker
          value-format="yyyy-MM-dd"
          style="width: 150px"
          v-model="payDate[1]"
          type="date"
          placeholder="选择结束日期"
        ></el-date-picker>
      </div>
      <el-button
        size="small"
        type="primary"
        icon="el-icon-search"
        class="handleSearch"
        @click="handleSearch()"
        >查询</el-button
      >
      <el-button
        size="small"
        type="danger"
        class="handleSearch"
        icon="el-icon-delete"
        @click="empty()"
        >清空</el-button
      >
      <el-button
        size="small"
        type="success"
        class="handleSearch"
        icon="el-icon-edit"
        @click="audit()"
        >审核通过</el-button
      >

      <el-button
        type="info"
        size="small"
        icon="el-icon-tickets"
        @click="derive()"
        >导出</el-button
      >
      <el-button
        v-if="acc_type != 1"
        icon="el-icon-circle-plus"
        size="small"
        type="primary"
        @click="addSettlement()"
        >添加负数结算单</el-button
      >
    </div>
    <div class="total">
      <div class="totalItem">
        <p class="totalItemName">应结金额：</p>
        <p class="totalItemText">{{ totalAmount.amount }}</p>
      </div>
      <div class="totalItem">
        <p class="totalItemName">已结金额：</p>
        <p class="totalItemText">{{ totalAmount.pay_amount }}</p>
      </div>
      <div class="totalItem">
        <p class="totalItemName">未结金额：</p>
        <p class="totalItemText">{{ totalAmount.unliquidated }}</p>
      </div>
      <div class="totalItem"  v-if="acc_type != 1&&acc_type != 6">
        <p class="totalItemName">不结算金额：</p>
        <p class="totalItemText">{{ totalAmount.un_pay_amount }}</p>
      </div>
    </div>
    <el-table
      max-height="600"
      :data="tableData"
      ref="multipleTable"
      tooltip-effect="dark"
      border
      style="width: 100%"
      empty-text="暂无数据"
      @selection-change="handleSelectionChange"
      class="hidden-xs-only"
      row-key="id"
      show-summary
      :summary-method="getSummaries"
    >
      <el-table-column
        type="selection"
        :selectable="selectable"
        :reserve-selection="true"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="xuhao"
        label="序号"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="serial_code"
        label="结算单号"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="amount"
        label="应结金额"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="pay_amount"
        label="已结金额"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="unliquidated"
        label="未结金额"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="statusDesc"
        label="财务结算状态"
        align="center"
      ></el-table-column>
      <el-table-column label="采购结算操作状态" width="160" align="center">
        <template slot-scope="scope">
          <el-switch
            :disabled="scope.row.gys_status == 1 || scope.row.cw_status == 2"
            v-model="scope.row.value"
            active-color="#ff4949"
            inactive-color="#13ce66"
            active-text="不结算"
            inactive-text="结算"
            @change="statusBtn(scope)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        prop="normal_desc"
        label="入库单结算状态"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="typeMsg"
        label="类型"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="codes"
        label="入库单/返货单"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="supplier_name"
        label="供应商"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="orgarea_name"
        label="区域"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="gys_time"
        label="供应商审核时间"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="contacts"
        label="名字"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="tel"
        label="手机号"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="class_code"
        label="品类编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="class_name"
        label="品类名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="buyer"
        label="采购员"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="buyer_code"
        label="采购员编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="one_class_name"
        label="一级分类"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="erp_goods_code"
        label="商品编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="goods_name"
        label="商品名"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="goods_num"
        label="商品数量"
        align="center"
      ></el-table-column>
      <el-table-column prop="qty" label="箱数" align="center"></el-table-column>
      <el-table-column
        prop="goods_price"
        label="商品价格"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="unit"
        label="单位"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="rdc_name"
        v-if="acc_type != 1"
        label="配送中心"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="handcode"
        label="手工单号"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="last_pay_time"
        label="付款时间"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="goods_num"
        label="本次结算数量"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="storage_num"
        label="应结总数量"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="stock_storage_num"
        label="剩余应结数量"
        align="center"
      ></el-table-column>
      <el-table-column label="操作" align="center" width="250px">
        <template slot-scope="scope">
          <el-button
            class="btn"
            type="warning"
            size="mini"
            icon="el-icon-back"
            v-if="showSuppler"
            @click="rollback(scope)"
            >回退</el-button
          >
          <el-button
            type="primary"
            size="mini"
            v-if="scope.row.showBtn[0] == 1"
            icon="el-icon-edit"
            @click="modification(scope)"
            >采购审核</el-button
          >
          <!-- <el-button
            type="primary"
            size="mini"
            v-if="scope.row.showBtn[2] == 1"
            icon="el-icon-edit"
            @click="modification(scope)"
            >供应商审核</el-button
          > -->
          <el-button
            v-if="acc_type != 1"
            type="info"
            size="mini"
            icon="el-icon-info"
            @click="particular(scope)"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-table
      :data="tableData"
      ref="multipleTables"
      tooltip-effect="dark"
      border
      style="width: 100%"
      empty-text="暂无数据"
      @selection-change="handleSelectionChange"
      row-key="id"
      show-summary
      :summary-method="getSummarie"
      class="hidden-sm-and-up"
    >
      <el-table-column
        type="selection"
        :selectable="selectable"
        :reserve-selection="true"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="serial_code"
        label="结算单号"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="amount"
        label="应结金额"
        align="center"
      ></el-table-column>

      <el-table-column
        prop="statusDesc"
        label="财务结算状态"
        align="center"
      ></el-table-column>
      <el-table-column label="采购结算操作状态" width="160" align="center">
        <template slot-scope="scope">
          <el-switch
            :disabled="scope.row.gys_status == 1 || scope.row.cw_status == 2"
            v-model="scope.row.value"
            active-color="#ff4949"
            inactive-color="#13ce66"
            active-text="不结算"
            inactive-text="结算"
            @change="statusBtn(scope)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        prop="supplier_name"
        label="供应商"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="gys_time"
        label="供应商审核时间"
        align="center"
      ></el-table-column>

      <el-table-column
        prop="goods_name"
        label="商品名"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="goods_num"
        label="商品数量"
        align="center"
      ></el-table-column>
      <el-table-column prop="qty" label="箱数" align="center"></el-table-column>
      <el-table-column
        prop="goods_price"
        label="商品价格"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="unit"
        label="单位"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="handcode"
        label="手工单号"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="last_pay_time"
        label="付款时间"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="goods_num"
        label="本次结算数量"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="storage_num"
        label="应结总数量"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="stock_storage_num"
        label="剩余应结数量"
        align="center"
      ></el-table-column>
      <el-table-column label="操作" align="center" width="250px">
        <template slot-scope="scope">
          <el-button
            class="btn"
            type="warning"
            size="mini"
            icon="el-icon-back"
            v-if="showSuppler"
            @click="rollback(scope)"
            >回退</el-button
          >
          <el-button
            type="primary"
            size="mini"
            v-if="scope.row.showBtn[0] == 1"
            icon="el-icon-edit"
            @click="modification(scope)"
            >采购审核</el-button
          >
          <!-- <el-button
            type="primary"
            size="mini"
            v-if="scope.row.showBtn[2] == 1"
            icon="el-icon-edit"
            @click="modification(scope)"
            >供应商审核</el-button
          > -->
          <el-button
            v-if="acc_type != 1"
            type="info"
            size="mini"
            icon="el-icon-info"
            @click="particular(scope)"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      title="备注"
      :visible.sync="remark.centerDialogVisible"
      center
      width="500px"
      @close="closes"
    >
      <el-form
        :model="formData"
        ref="formData"
        label-width="80px"
        label-position="left"
      >
        <el-form-item label="备注：">
          <el-input
            type="textarea"
            v-model="formData.remark"
            placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 分页 -->
    <pagination
      @page="handleSizeChange"
      @pagesize="handleCurrentChange"
      :pageData="pageData"
    ></pagination>
    <div style="height: 0">
      <supplierSearch
        :supplierdialog="supplierdialog"
        @tag="supplierTag"
      ></supplierSearch>
      <commodityDialog
        :commoditydialog="commoditydialog"
        @tag="commodityTag"
      ></commodityDialog>
      <classDialog :classdialog="classdialog" @tag="classTag"></classDialog>
      <!-- dialog -->
      <settlementDialog :dialog="dialog"></settlementDialog>
      <settlementParticulars :dialogs="dialogs"></settlementParticulars>
      <minusDialog :minusDialog="minusDialog" @upData="getData"></minusDialog>
      <auditListDialog
        @upData="getData"
        :dialog="auditDialog"
        :tableData="auditTabData"
        :acc_type="acc_type"
        :showSuppler="showSuppler"
      ></auditListDialog>
    </div>
  </div>
</template>
<script>
import { getToken } from "@/utils/auth";
import { getnowDate } from "@/utils/index";
import SupplierSearch from "@/components/grossMargin/supplierSearch";
import CommodityDialog from "@/components/grossMargin/commodityDialog";
import { supplierList } from "@/api/supplier/supplierList";
import { getInfo } from "@/api/user";
import SettlementDialog from "@/components/settlement/settlementDialog";
import SettlementParticulars from "@/components/settlement/settlementParticulars";
import ClassDialog from "@/components/grossMargin/classDialog";
import Pagination from "@/components/pagination";
import MinusDialog from "@/components/settlement/minusDialog";
import AuditListDialog from "@/components/settlement/auditListDialog";

import {
  selectStatementList,
  platConfirm,
  supplierConfirm,
  confirmStatement,
  supplierBackCongirm,
  saveStatementStatus,
  selectMinusStatementList
} from "@/api/settlement/settlementList";
import { getAreaList } from "@/api/permission/adminlist";
export default {
  components: {
    pagination: Pagination,
    settlementDialog: SettlementDialog,
    supplierSearch: SupplierSearch,
    commodityDialog: CommodityDialog,
    settlementParticulars: SettlementParticulars,
    classDialog: ClassDialog,
    minusDialog: MinusDialog,
    auditListDialog: AuditListDialog
  },
  data() {
    return {
      //页面数据
      tableData: [],
      auditTabData: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper"
      },
      total: null,
      date: [],
      dates: [],
      payDate: [],
      //dialog信息
      dialog: {
        centerDialogVisible: false,
        id: ""
      },
      minusDialog: {
        centerDialogVisible: false
      },
      auditDialog: {
        centerDialogVisible: false
      },
      //默认时间
      normal_startime: getnowDate().settlementStarTimer,
      normal_endtime: getnowDate().endtimer,
      search: {
        examine_status: "",
        searchCode: "",
        start_time: "",
        end_time: "",
        gys_start_date: "",
        gys_end_date: "",
        area: "",
        pay_serial_code: "",
        pay_start_date: "",
        pay_end_date: ""
      },
      insideArea: {
        innerVisible: false,
        options: [],
        value: "",
        list: [],
        loading: false
      },
      options: [
        {
          value: 1,
          label: "未结算"
        },
        {
          value: 2,
          label: "已结算"
        }
      ],
      option: [],
      formData: {
        remark: ""
      },
      remark: {
        id: "",
        sum: "",
        centerDialogVisible: false
      },
      //dialog信息
      dialogs: {
        centerDialogVisible: false,
        id: ""
      },
      // auditStatus: false,
      price: 0,
      totalAmount: {},
      //dialog信息
      supplierdialog: {
        centerDialogVisible: false
      },
      commoditydialog: {
        centerDialogVisible: false
      },
      classdialog: {
        centerDialogVisible: false
      },
      supplierNameList: {
        name: "",
        code: ""
      },
      classNameList: {
        name: "",
        id: ""
      },
      commodityNameList: {
        name: "",
        code: ""
      },
      showSuppler: false,
      acc_type: null,
      selectedData: []
    };
  },
  created() {
    this.search.start_time = this.normal_startime;
    this.search.end_time = this.normal_endtime;
    if (this.normal_startime) {
      this.date.push(this.search.start_time);
    }
    if (this.normal_endtime) {
      this.date.push(this.search.end_time);
    }
    this.getInfos();
    this.getDatas();
  },
  methods: {
    getInfos() {
      getInfo()
        .then(res => {
          if (res.code == 0) {
            this.acc_type = res.data.admin_info.acc_type;
            if (res.data.admin_info.acc_type == 3) {
              this.search.examine_status = [1];
            } else if (res.data.admin_info.acc_type == 1) {
              this.search.examine_status = [11];
            } else {
              this.search.examine_status = [];
            }
          }
        })
        .then(() => {
          this.getData();
        });
    },
    //获取页面数据
    getData(bool = null) {
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        supplier_id: this.supplierNameList.code,
        goods_id: this.commodityNameList.code,
        class_id: this.classNameList.id,
        serial_code: this.search.searchCode,
        start_date: this.search.start_time,
        end_date: this.search.end_time,

        gys_start_date: this.search.gys_start_date,
        gys_end_date: this.search.gys_end_date,
        orgarea_code_str: this.search.area,
        examine_status: this.search.examine_status.join(","),
        pay_serial_code: this.search.pay_serial_code,
        pay_start_date: this.search.pay_start_date,
        pay_end_date: this.search.pay_end_date
      };
      selectStatementList(data)
        .then(res => {
          if (res.code == 0) {
            res.data.data.forEach(res => {
              if (res.cw_status == 0) {
                res.value = false;
              } else {
                res.value = true;
              }
              res.statusMsg = res.status == 1 ? "未结算" : "已结算";
              if (res.kind == 10) {
                res.typeMsg = "返货单结算单";
              } else if (res.kind == 11) {
                res.typeMsg = "手动入库单";
              } else if (res.kind == 12 || res.kind == 13) {
                res.typeMsg = "费用结算单";
              } else if (res.kind == 81 || res.kind == 82) {
                res.typeMsg = "改账结算单";
              } else{
                res.typeMsg = "入库单结算单";
              }
              res.relevance =
                res.type == 0 ? res.storage_bill_id : res.return_id;
              res.codes = res.kind == 10 ? res.return_code : res.storage_code;
              switch (res.cw_status) {
                case 0:
                  res.cw_statusMsg = "正常";
                  break;
                case 1:
                  res.cw_statusMsg = "冻结";
                  break;
                case 2:
                  res.cw_statusMsg = "不结算";
                  break;
              }
            });
            if (res.data.acc_type == 3) {
              this.option = [
                {
                  value: 1,
                  label: "采购待审核"
                },
                {
                  value: 2,
                  label: "采购审核通过"
                },
                {
                  value: 21,
                  label: "待结算"
                },
                {
                  value: 22,
                  label: "结算完成"
                },
                {
                  value: 23,
                  label: "部分结算"
                },
                {
                  value: 32,
                  label: "冻结"
                },
                {
                  value: 33,
                  label: "不结算"
                }
              ];
            } else if (res.data.acc_type == 1) {
              this.option = [
                {
                  value: 11,
                  label: "供应商待审核"
                },
                {
                  value: 12,
                  label: "供应商审核通过"
                },
                {
                  value: 21,
                  label: "待结算"
                },
                {
                  value: 22,
                  label: "结算完成"
                },
                {
                  value: 23,
                  label: "部分结算"
                },
                {
                  value: 32,
                  label: "冻结"
                },
                {
                  value: 33,
                  label: "不结算"
                }
              ];
            } else {
              this.option = [
                {
                  value: 1,
                  label: "采购待审核"
                },
                {
                  value: 2,
                  label: "采购审核通过"
                },

                {
                  value: 11,
                  label: "供应商待审核"
                },
                {
                  value: 12,
                  label: "供应商审核通过"
                },
                {
                  value: 21,
                  label: "待结算"
                },
                {
                  value: 22,
                  label: "结算完成"
                },
                {
                  value: 23,
                  label: "部分结算"
                },
                {
                  value: 32,
                  label: "冻结"
                },
                {
                  value: 33,
                  label: "不结算"
                }
              ];
            }

            this.showSuppler = res.data.is_supplier;
            this.acc_type = res.data.acc_type;
            this.tableData = res.data.data;
            this.pageData.page = res.data.current_page;
            this.pageData.pagesize = res.data.per_page;
            this.pageData.total = res.data.total;
            this.totalAmount = res.amountTo;
            if (bool) {
              this.$refs.multipleTable.clearSelection();
              this.$refs.multipleTables.clearSelection();
            }
            this.lists = res.data.data.filter(result => {
              if (res.data.acc_type == 0 || res.data.acc_type == 3) {
                return (
                  result.amount < 0 &&
                  result.pt_status != 1 &&
                  result.cw_status == 0
                );
              } else if (res.data.acc_type == 1) {
                return result.amount < 0 && result.gys_status != 1;
              }
            });
          }
        })
        .then(() => {
          this.lists.forEach((res, index) => {
            this.$refs.multipleTable.toggleRowSelection(
              this.lists[index],
              true
            );
          });
        })
        .then(() => {
          this.lists.forEach((res, index) => {
            this.$refs.multipleTables.toggleRowSelection(
              this.lists[index],
              true
            );
          });
        })
        .catch(err => {
          console.log(err);
        });
    },
    //添加负数结算单
    addSettlement() {
      this.minusDialog.centerDialogVisible = true;
    },
    getDatas() {
      let data = {
        page: 1,
        limit: 50
      };

      //获取初始化区域列表
      getAreaList(data).then(res => {
        if (res.data.listdata) {
          this.insideArea.options = res.data.listdata.map(item => {
            return { value: `${item.code}`, label: `${item.name}` };
          });
        }
      });
    },
    //不结算状态操作
    statusBtn(scope, sum) {
      if (scope.row.value) {
        this.remark.sum = 2;
      } else {
        this.remark.sum = 0;
      }
      this.remark.id = scope.row.id;
      this.remark.centerDialogVisible = true;
    },
    //关闭备注弹窗
    closes() {
      this.getData();
      this.formData.remark = "";
      this.remark.centerDialogVisible = false;
    },
    //弹窗确定
    confirm() {
      let data = {
        id: this.remark.id,
        cw_status: this.remark.sum,
        remark: this.formData.remark
      };
      saveStatementStatus(data)
        .then(res => {
          if (res.code == 0) {
            this.$message({
              type: "success",
              message: res.msg || res.message
            });
            this.remark.centerDialogVisible = false;
            this.getData();
          } else {
            this.$message({
              type: "error",
              message: res.msg || res.message
            });
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    //详情
    particular(scope) {
      this.dialogs.centerDialogVisible = true;
      this.dialogs.id = scope.row.id;
    },
    // 搜索区域
    insideRemoteMethods(query) {
      if (query !== "") {
        this.getSupplierLists(query);
      } else {
        this.insideArea.options = [];
      }
    },
    getSupplierLists(query) {
      let data = {
        name: query,
        page: 1,
        limit: 100
      };
      getAreaList(data).then(res => {
        this.insideArea.list = res.data.listdata.map(item => {
          return { value: `${item.code}`, label: `${item.name}` };
        });
        this.insideArea.options = this.insideArea.list.filter(item => {
          this.insideArea.loading = false;
          return item.label.toLowerCase().indexOf(query.toLowerCase()) > -1;
        });
      });
    },
    //超级供应商搜索
    supplierBtn() {
      this.supplierdialog = {
        centerDialogVisible: true
      };
    },
    //超级商品搜索
    commodityBtn() {
      this.commoditydialog = {
        centerDialogVisible: true
      };
    },
    //超级类别搜索
    classBtn() {
      this.classdialog = {
        centerDialogVisible: true
      };
    },
    classTag(data) {
      this.classNameList = {
        name: "",
        id: ""
      };
      let arrName = [];
      let arrCode = [];
      data.forEach(res => {
        arrName.push(res.name);
        arrCode.push(res.id);
      });
      this.classNameList.name = arrName.join(",");
      this.classNameList.id = arrCode.join(",");
    },
    supplierTag(data) {
      this.supplierNameList = {
        name: "",
        code: ""
      };
      let arrName = [];
      let arrCode = [];
      data.forEach(res => {
        arrName.push(res.name);
        arrCode.push(res.code);
      });
      this.supplierNameList.name = arrName.join(",");
      this.supplierNameList.code = arrCode.join(",");
    },
    commodityTag(data) {
      this.commodityNameList = {
        name: "",
        code: ""
      };
      let arrName = [];
      let arrCode = [];
      data.forEach(res => {
        arrName.push(res.name);
        arrCode.push(res.id);
      });
      this.commodityNameList.name = arrName.join(",");
      this.commodityNameList.code = arrCode.join(",");
    },
    //合计
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        const values = data.map(item => Number(item[column.property]));
        if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          if (index != 5 && index != 3 && index != 4) {
            sums[index] = "";
          } else {
            sums[index] = sums[index].toFixed(4);
          }
        } else {
          sums[index] = "";
        }
      });

      return sums;
    },
    //移动端合计
    getSummarie(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        const values = data.map(item => Number(item[column.property]));
        if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          if (index != 2) {
            sums[index] = "";
          } else {
            sums[index] = sums[index].toFixed(4);
          }
        } else {
          sums[index] = "";
        }
      });

      return sums;
    },
    //审核结算
    modification(scope) {
      let data = {
        id: scope.row.id
      };
      this.$confirm("是否审核结算该条信息?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          if (scope.row.showBtn[0] == 1) {
            data.pt_status = 1;
            platConfirm(data).then(res => {
              if (res.code == 0) {
                this.getData();
                this.$message({
                  type: "success",
                  message: res.msg
                });
              }
            });
          } else if (scope.row.showBtn[2] == 1) {
            data.gys_status = 1;
            supplierConfirm(data).then(res => {
              if (res.code == 0) {
                this.getData();
                this.$message({
                  type: "success",
                  message: res.msg
                });
              }
            });
          } else if (scope.row.showBtn[4] == 1) {
            data.cw_status = 1;
            confirmStatement(data).then(res => {
              if (res.code == 0) {
                this.getData();
                this.$message({
                  type: "success",
                  message: res.msg
                });
              }
            });
          }
        })

        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消审核结算"
          });
        });
    },

    //批量审核通过
    audit() {
      selectMinusStatementList()
        .then(res => {
          if (res.data.list.length > 0) {
            res.data.list.forEach(res => {
              if (res.cw_status == 0) {
                res.value = false;
              } else {
                res.value = true;
              }
              res.statusMsg = res.status == 1 ? "未结算" : "已结算";
              if (res.kind == 10) {
                res.typeMsg = "返货单结算单";
              } else if (res.kind == 11) {
                res.typeMsg = "手动入库单";
              } else if (res.kind == 12 || res.kind == 13) {
                res.typeMsg = "费用结算单";
              } else if (res.kind == 81 || res.kind == 82) {
                res.typeMsg = "改账结算单";
              } else{
                res.typeMsg = "入库单结算单";
              }
              res.relevance =
                res.type == 0 ? res.storage_bill_id : res.return_id;
              res.codes = res.kind == 10 ? res.return_code : res.storage_code;
              switch (res.cw_status) {
                case 0:
                  res.cw_statusMsg = "正常";
                  break;
                case 1:
                  res.cw_statusMsg = "冻结";
                  break;
                case 2:
                  res.cw_statusMsg = "不结算";
                  break;
              }
            });
            if (res.data.acc_type == 3) {
              this.option = [
                {
                  value: 1,
                  label: "采购待审核"
                },
                {
                  value: 2,
                  label: "采购审核通过"
                },
                {
                  value: 21,
                  label: "待结算"
                },
                {
                  value: 22,
                  label: "结算完成"
                },
                {
                  value: 23,
                  label: "部分结算"
                },
                {
                  value: 32,
                  label: "冻结"
                },
                {
                  value: 33,
                  label: "不结算"
                }
              ];
            } else if (res.data.acc_type == 1) {
              this.option = [
                {
                  value: 11,
                  label: "供应商待审核"
                },
                {
                  value: 12,
                  label: "供应商审核通过"
                },
                {
                  value: 21,
                  label: "待结算"
                },
                {
                  value: 22,
                  label: "结算完成"
                },
                {
                  value: 23,
                  label: "部分结算"
                },
                {
                  value: 32,
                  label: "冻结"
                },
                {
                  value: 33,
                  label: "不结算"
                }
              ];
            } else {
              this.option = [
                {
                  value: 1,
                  label: "采购待审核"
                },
                {
                  value: 2,
                  label: "采购审核通过"
                },

                {
                  value: 11,
                  label: "供应商待审核"
                },
                {
                  value: 12,
                  label: "供应商审核通过"
                },
                {
                  value: 21,
                  label: "待结算"
                },
                {
                  value: 22,
                  label: "结算完成"
                },
                {
                  value: 23,
                  label: "部分结算"
                },
                {
                  value: 32,
                  label: "冻结"
                },
                {
                  value: 33,
                  label: "不结算"
                }
              ];
            }
            this.auditTabData = res.data.list;
            return true;
          } else {
            return false;
          }
        })
        .then(bool => {
          if ((bool && this.acc_type == 1) || (bool && this.acc_type == 3)) {
            this.auditDialog.centerDialogVisible = true;
          } else {
            if (this.selectedData.length == 0) {
              this.$message({
                type: "info",
                message: "请勾选结算单"
              });
            } else {
              this.$confirm("是否审核通过?", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
              })
                .then(() => {
                  let idArr = [];
                  this.selectedData.forEach(res => {
                    idArr.push(res.id);
                  });
                  let ids = idArr.join(",");
                  let data = {
                    id: ids
                  };
                  if (
                    this.acc_type == 0 ||
                    this.acc_type == 3 ||
                    this.acc_type == 8
                  ) {
                    data.pt_status = 1;
                    platConfirm(data).then(res => {
                      if (res.code == 0) {
                        this.$refs.multipleTable.clearSelection();
                        this.$refs.multipleTables.clearSelection();
                        this.getData();
                        this.$message({
                          type: "success",
                          message: res.msg
                        });
                      }
                    });
                  } else if (this.acc_type == 1) {
                    data.gys_status = 1;
                    supplierConfirm(data).then(res => {
                      if (res.code == 0) {
                        this.$refs.multipleTable.clearSelection();
                        this.$refs.multipleTables.clearSelection();
                        this.getData();
                        this.$message({
                          type: "success",
                          message: res.msg
                        });
                      }
                    });
                  }
                })
                .catch(() => {
                  this.$message({
                    type: "info",
                    message: "已取消审核结算"
                  });
                });
            }
          }
        });
    },

    //回退
    rollback(scope) {
      let data = {
        id: scope.row.id
      };
      this.$confirm("是否回退?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          supplierBackCongirm(data).then(res => {
            if (res.code == 0) {
              this.getData();
              this.$message({
                type: "success",
                message: res.msg || res.message
              });
            } else {
              this.$message({
                type: "error",
                message: res.msg || res.message
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },

    //导出
    derive() {
      this.$confirm("是否导出?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let link = document.createElement("a");
          let examine_status = this.search.examine_status.join(",");

          if (this.date.length > 0) {
            this.search.start_time = this.date[0];
            this.search.end_time = this.date[1];
          }
          if (this.dates.length > 0) {
            this.search.gys_start_date = this.dates[0];
            this.search.gys_end_date = this.dates[1];
          }
          if (this.payDate.length > 0) {
            this.search.pay_start_date = this.payDate[0];
            this.search.pay_end_date = this.payDate[1];
          }
          link.href =
            this.$axios.defaults.baseURL +
            "/api/statement/exportStatementList?token=" +
            getToken() +
            "&class_id=" +
            this.classNameList.id +
            "&supplier_id=" +
            this.supplierNameList.code +
            "&goods_id=" +
            this.commodityNameList.code +
            "&examine_status=" +
            examine_status +
            "&serial_code=" +
            this.search.searchCode +
            "&pay_serial_code=" +
            this.search.pay_serial_code +
            "&pay_start_date=" +
            this.search.pay_start_date +
            "&pay_end_date=" +
            this.search.pay_end_date +
            "&start_date=" +
            this.search.start_time +
            "&end_date=" +
            this.search.end_time +
            "&gys_start_date=" +
            this.search.gys_start_date +
            "&gys_end_date=" +
            this.search.gys_end_date +
            "&orgarea_code_str=" +
            this.search.area;
          link.click();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消导出"
          });
        });
    },
    //详情
    particulars(scope) {
      this.dialog.centerDialogVisible = true;
      this.dialog.id = scope.row.id;
    },
    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;
      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;
      this.getData();
    },
    //不让勾选操作
    selectable(row, index) {
      if (this.acc_type == 0 || this.acc_type == 3) {
        if (
          row.pt_status == 1 ||
          row.amount < 0 ||
          row.showBtn[6] == 0 ||
          row.states == "停用" ||
          row.states == "冻结账款"
        ) {
          return false;
        } else {
          return true;
        }
      } else if (this.acc_type == 1) {
        if (
          row.gys_status == 1 ||
          row.amount < 0 ||
          row.states == "停用" ||
          row.states == "冻结账款"
        ) {
          return false;
        } else {
          return true;
        }
      } else {
        return true;
      }
    },
    // 搜索
    handleSearch() {
      if (this.date.length > 0) {
        this.search.start_time = this.date[0];
        this.search.end_time = this.date[1];
      }
      if (this.dates.length > 0) {
        this.search.gys_start_date = this.dates[0];
        this.search.gys_end_date = this.dates[1];
      }
      if (this.payDate.length > 0) {
        this.search.pay_start_date = this.payDate[0];
        this.search.pay_end_date = this.payDate[1];
      }
      this.pageData.page = 1;
      this.pageData.total = null;
      this.$refs.multipleTable.clearSelection();
      this.getData();
    },
    //清空
    empty() {
      this.date = [];
      this.dates = [];
      this.payDate = [];
      this.date.push(this.normal_startime);
      this.date.push(this.normal_endtime);
      this.search = {
        productclass_id: "",
        examine_status: [],
        searchCode: "",
        start_time: this.normal_startime,
        end_time: this.normal_endtime,
        gys_start_date: "",
        gys_end_date: "",
        area: "",
        pay_serial_code: "",
        pay_start_date: "",
        pay_end_date: ""
      };
      // this.examine_status = true;
      if (this.acc_type == 3) {
        this.search.examine_status = [1];
      } else if (this.acc_type == 1) {
        this.search.examine_status = [11];
      } else {
        this.search.examine_status = [];
      }
      this.insideArea = {
        innerVisible: false,
        options: [],
        value: "",
        list: [],
        loading: false
      };
      this.getDatas();
      this.supplierNameList = {
        name: "",
        code: ""
      };
      this.commodityNameList = {
        name: "",
        code: ""
      };
      this.classNameList = {
        name: "",
        id: ""
      };
    },
    //选取
    handleSelectionChange(val) {
      this.price = 0;
      this.selectedData = val;
      this.selectedData.forEach(res => {
        this.price += parseFloat(res.amount);
      });
      this.price = this.price.toFixed(4);
    }
  },
  computed: {
    checkedWeight() {
      let totalWeight = 0;
      this.selectedData.forEach(item => {
        totalWeight += item.weight;
      });
      return totalWeight.toFixed(2);
    }
  }
};
</script>
<style scoped>
.top {
  position: relative;
  margin-bottom: 5px;
}
.top .el-select,
.top .el-input {
  margin-bottom: 5px !important;
}
.price {
  -webkit-appearance: none;
  background-color: #fff;
  background-image: none;
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  color: #606266;
  display: inline-block;
  font-size: inherit;
  height: 40px;
  line-height: 40px;
  outline: 0;
  padding: 0 15px;
  -webkit-transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  width: 100px;
  margin-left: 5px;
}
.add {
  position: absolute;
  right: 0;
  top: 4px;
}
.pagination {
  text-align: right;
  padding: 10px;
}
.total {
  display: flex;
  margin-top: 10px;
  margin-bottom: 10px;
  width: 100%;
}
.total .totalItem {
  margin-right: 20px;
  font-size: 14px;
}

@media screen and (min-width: 750px) {
  .total .totalItem {
    margin-right: 20px;
    font-size: 14px;
    display: flex;
  }
}
.btn {
  margin-bottom: 5px;
}
</style>
